package com.zcc.blog.controller;

import com.zcc.blog.dto.BlogCategoryDto;
import com.zcc.blog.service.BlogService;
import com.zcc.blog.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zcc
 * @since 2022-09-05
 */
@Api(tags = "博客")
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    BlogService blogService;

    @ApiOperation("分页查找博客")
    @GetMapping("/blogs")
    public Result findBlogs(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, Long id){
        return blogService.findBlogs(currentPage, size, id, 0);
    }

    @ApiOperation("条件查找博客")
    @GetMapping("/search")
    public Result searchBlogs(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, String title, String description, String content, Long id){
        return blogService.searchBlogs(title, description, content, currentPage, size, id, 0);
    }

    @ApiOperation("按时间顺序排序博客")
    @GetMapping("/sortOrder")
    public Result orderSort(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, Long id){
        return blogService.sortOrder(currentPage, size, id);
    }

    @ApiOperation("按时间顺序逆序博客")
    @GetMapping("/sortReversed")
    public Result sortReversed(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, Long id){
        return blogService.sortReversed(currentPage, size, id);
    }

    @ApiOperation("通过id查找博客")
    @GetMapping("/find/{id}")
    public Result detail(@PathVariable Long id){
        return blogService.findBlogById(id);
    }

    @ApiOperation("新增博客")
    @RequiresAuthentication
    @PostMapping("/add")
    public Result addBlog(@Validated @RequestBody BlogCategoryDto blogCategoryDto){
        return blogService.addBlog(blogCategoryDto);
    }

    @ApiOperation("更新博客")
    @RequiresAuthentication
    @PutMapping("/update")
    public Result updateBlog(@Validated @RequestBody BlogCategoryDto blogCategoryDto){
        return blogService.updateBlog(blogCategoryDto);
    }

    @ApiOperation("删除博客")
    @RequiresAuthentication
    @PutMapping("/delete/{id}")
    public Result deleteBlog(@PathVariable Long id){
        return blogService.deleteBlog(id);
    }

    @ApiOperation("开启自我推荐博客")
    @RequiresAuthentication
    @PutMapping("/openRecommend/{id}")
    public Result openRecommend(@PathVariable Long id){
        return blogService.openRecommend(id);
    }

    @ApiOperation("关闭自我推荐博客")
    @RequiresAuthentication
    @PutMapping("/closeRecommend/{id}")
    public Result closeRecommend(@PathVariable Long id){
        return blogService.closeRecommend(id);
    }

    @ApiOperation("展示推荐的博客")
    @GetMapping("/showRecommend")
    public Result showRecommend(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size){
        return blogService.findRecommendBlogs(currentPage, size);
    }

    @ApiOperation("统计每个月的博客发布量")
    @GetMapping("/countBlogsByMonth/{id}")
    public Result countBlogsByMonth(@PathVariable Long id) {
        return blogService.countBlogsByMonth(id);
    }

    @ApiOperation("统计用户的所有博客发布量")
    @GetMapping("/countBlogs/{id}")
    public Result countBlogs(@PathVariable Long id) {
        return blogService.countBlogs(id);
    }

}
